﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.IO;

namespace PefStudio.SegyViewer
{
    class SegyViewerSetting
    {
        public static Dictionary<string, List<HeaderField>> TraceHeaderFieldSets = new Dictionary<string, List<HeaderField>>();
        public static Dictionary<string, List<HeaderField>> ReelHeaderFieldSets = new Dictionary<string, List<HeaderField>>();

        static SegyViewerSetting()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Path.GetDirectoryName(typeof(SegyViewerSetting).Assembly.Location) + @"\TraceSets.xml");

            XmlNodeList nodesTraceheader = xmlDoc.SelectNodes("TraceSets/traceheader_extract_style");
            foreach (XmlNode nodeTraceheader in nodesTraceheader)
            {
                string traceheader_name = nodeTraceheader.SelectSingleNode("name").InnerText;

                XmlNodeList nodesFields = nodeTraceheader.SelectNodes("fields/field");

                List<HeaderField> traceheaderFields = new List<HeaderField>();

                foreach (XmlNode nodeField in nodesFields)
                {
                    HeaderField field = new HeaderField();
                    field.Name = nodeField.SelectSingleNode("name").InnerText;
                    field.Size = int.Parse(nodeField.SelectSingleNode("size").InnerText);
                    if (nodeField.SelectSingleNode("issamplepoint") != null)
                        field.IsSamplePoint = bool.Parse(nodeField.SelectSingleNode("issamplepoint").InnerText);
                    if (nodeField.SelectSingleNode("description") != null)
                        field.Description = nodeField.SelectSingleNode("description").InnerText;

                    traceheaderFields.Add(field);
                }

                TraceHeaderFieldSets.Add(traceheader_name, traceheaderFields);
            }

            XmlNodeList nodesReelheader = xmlDoc.SelectNodes("TraceSets/reelheader_extract_style");
            foreach (XmlNode nodeReelheader in nodesReelheader)
            {
                string reelheader_name = nodeReelheader.SelectSingleNode("name").InnerText;

                XmlNodeList nodesFields = nodeReelheader.SelectNodes("fields/field");

                List<HeaderField> reelheaderFields = new List<HeaderField>();

                foreach (XmlNode nodeField in nodesFields)
                {
                    HeaderField field = new HeaderField();
                    field.Name = nodeField.SelectSingleNode("name").InnerText;
                    field.Size = int.Parse(nodeField.SelectSingleNode("size").InnerText);
                    if (nodeField.SelectSingleNode("description") != null)
                        field.Description = nodeField.SelectSingleNode("description").InnerText;

                    reelheaderFields.Add(field);
                }

                ReelHeaderFieldSets.Add(reelheader_name, reelheaderFields);
            }
        }

        /// <summary>
        /// Returns a File Dialog filter that can be used to filter on all registered segy formats
        /// </summary>
        //public static string GetAllSegysFilter()
        //{
        //    AddInTreeNode addinTreeNode = AddInTree.GetTreeNode("/SharpStudio/Workbench/FileBrowserFilter");
        //    StringBuilder b = new StringBuilder();

        //    foreach (Codon c in addinTreeNode.Codons)
        //    {
        //        if (c.Id.StartsWith("Segy") == false)
        //            continue;

        //        b.Append(c.Properties.Get("name", ""));
        //        b.Append('|');
        //        b.Append(c.Properties.Get("extensions", ""));
        //        b.Append('|');
        //    }

        //    foreach (Codon c in addinTreeNode.Codons)
        //    {
        //        if (c.Id.Equals("All") == true)
        //        {
        //            b.Append(c.Properties.Get("name", ""));
        //            b.Append('|');
        //            b.Append(c.Properties.Get("extensions", ""));
        //        }
        //    }
        //    return b.ToString();
        //}
    }
}
;